package com.yequan.springmvc.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yequan.springmvc.handler.UserAddHandler;
import com.yequan.springmvc.handler.UserQueryHandler;
import com.yequan.springmvc.handler.iface.HttpServletRequestHandler;
import com.yequan.springmvc.handler.iface.SimpleControllerHandler;

/**
 * 
 * @author 灭霸詹
 *
 */
public class DispatcherServlet extends AbstractsHttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 核心逻辑
	 * 
	 * @param request
	 * @param response
	 */
	public void doDispatch(HttpServletRequest request, HttpServletResponse response) {
		try {
			// 查找处理器
			String uri = request.getRequestURI();
			Object handler = null;
			if ("/userAdd".equals(uri)) {
				// HttpServletRequestHandler handler = new UserAddHandler();
				// handler.handleRequest(request, response);
				handler = new UserAddHandler();
			} else if ("/userQuery".equals(uri)) {
				// SimpleControllerHandler handler = new UserQueryHandler();
				// handler.handleRequest(request, response);
				handler = new UserQueryHandler();
			}
			// 执行处理器
			if (handler instanceof HttpServletRequestHandler) {
				((HttpServletRequestHandler) handler).handleRequest(request, response);
			} else if (handler instanceof SimpleControllerHandler) {
				((SimpleControllerHandler) handler).handleRequest(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
